// SetPathDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FilesTransInNet.h"
#include "SetPathDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "UserMsg.h"

/////////////////////////////////////////////////////////////////////////////
// CSetPathDlg dialog
int CALLBACK _SHBrowseForFolderCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	if(uMsg == BFFM_INITIALIZED)
	{
		::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);	
	} 
	
	return 0;
}


CSetPathDlg::CSetPathDlg(CString strOldPath, CString strTitle, CWnd* pParent /*=NULL*/)
	: CDialog(CSetPathDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetPathDlg)
	m_strNewPath = _T("");
	//}}AFX_DATA_INIT
	m_strTitle = strTitle;
	m_strCurPath = strOldPath;
}


void CSetPathDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetPathDlg)
	DDX_Text(pDX, IDC_PATH, m_strNewPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetPathDlg, CDialog)
	//{{AFX_MSG_MAP(CSetPathDlg)
	ON_BN_CLICKED(IDC_BTNVIEW, OnBtnview)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetPathDlg message handlers

void CSetPathDlg::OnBtnview() 
{
	// TODO: Add your control notification handler code here
	char szFolderName[MAX_PATH] = {0};
	char szInitFolder[MAX_PATH] = {0};
	char *pszInitFolder = NULL;
	
	pszInitFolder = CSTR2CHAR(m_strCurPath);
	
	if (0 == this->GetFolder_Dialog (this->m_hWnd, CSTR2CHAR(m_strTitle), szFolderName, pszInitFolder))
	{
		m_strNewPath = szFolderName;
		m_strCurPath = m_strNewPath;
	}

	return ;	
}

int CSetPathDlg::GetPath(CString &strPath)
{
	strPath = m_strCurPath;
	return 0;
}

int CSetPathDlg::GetFolder_Dialog(HWND hWnd, char *szTipInfo, char *szFolderName, char *szInitFolder)
{
	if( (NULL == szTipInfo) || (NULL == szFolderName) )
	{
		return -1;
	}
	
	BROWSEINFO bi;
	
	memset (&bi, 0, sizeof(BROWSEINFO));
	bi.hwndOwner = hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = NULL;
	bi.lpszTitle = szTipInfo;
	bi.ulFlags = BIF_RETURNONLYFSDIRS/* | BIF_NEWDIALOGSTYLE*/;
	bi.lpfn = NULL;
	bi.lParam = NULL;
	bi.iImage = 0;
	
	if (szInitFolder)
	{
		bi.lpfn = _SHBrowseForFolderCallbackProc;
		bi.lParam = (LPARAM)szInitFolder;	
	}
	
	ITEMIDLIST *il = SHBrowseForFolder (&bi);
	if (NULL == il)
	{
		return -2; 
	}	
	
	SHGetPathFromIDList (il, szFolderName);
	
	return 0;
}
